package com.lib724.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lib724.dao.impl.Facade;
import com.lib724.model.User;

public class FacadeImpl implements Facade {

    // a very simple in-memory database:
    private Map<String, User> users = Collections.synchronizedMap(new HashMap<String, User>());

    public void createUser(User user) {
        if(users.containsKey(user.getUsername()))
            throw new RuntimeException("User exist!");
        users.put(user.getUsername(), user);
    }

    public User queryUser(String username) {
        User user = users.get(username);
        if(user==null)
            throw new RuntimeException("User not found: " + username);
        return user;
    }

    public List<User> queryUsers() {
        List<User> list = new ArrayList<User>(10);
        list.addAll(users.values());
        return list;
    }

}
